package com.atguigui.leetcode1;

/**
 * 415. 字符串相加
 * Project: leetcode
 * Package: com.atguigui.leetcode1
 * Version: 1.0
 * <p>
 * Created by WJX on 2023/7/17 9:16
 */
public class P415AddStrings {
    public static void main(String[] args) {
        Solution solution = new P415AddStrings().new Solution();
        String str1 = "11";
        String str2 = "123";
        System.out.println(solution.addStrings(str1, str2));
    }

    class Solution {
        public String addStrings(String num1, String num2) {
            int n = num1.length();
            int m = num2.length();

            int add = 0;

            StringBuilder ans = new StringBuilder();

            int a = 1, b = 1;

            while (a <= n || b <= m) {
                Integer x = 0;
                Integer y = 0;

                if (a <= n) {
                    x = Integer.valueOf(num1.substring(n - a, n - a + 1));
                }
                if (b <= m) {
                    y = Integer.valueOf(num2.substring(m - b, m - b + 1));
                }

                int sum = 0;

                if (add > 0) {
                    sum++;
                }

                sum += x + y;
                if (sum >= 10) {
                    add = 1;
                } else {
                    add = 0;
                }

                ans.append(sum % 10);
                a++;
                b++;
            }
            if (add > 0) {
                ans.append("1");
            }


            return ans.reverse().toString();
        }
    }
}
